$(function(){
	//查询分公司
	branch(null);
})

function branch(data){
	var url="../../sumchase/selectAllBranch.action";
	$.post(url,data,function(mes){
		$.each(mes,function(i,item){
			$("#braId").append("<option value="+item.braId+">"+item.braName+"</option>");
			load();
		})
	},"json");
}

layui.use(['form', 'layedit', 'laydate','table'], function(){
	 var table = layui.table
	  ,laydate = layui.laydate
	  ,form = layui.form
	  ,layer = layui.layer
	  ,layedit = layui.layedit;
	 
	 //监听工具条
	  table.on('tool(demo)', function(obj){
		  if(obj.event === 'spxq'){
				 layer.open({
		 	        type: 1	 //此处以iframe举例
		 	        ,title: '商品详情'
		 	        ,offset: 'auto'
		 	        ,area: ['1000px', '490px']
		 	        ,content:$('#add')
		 	        ,success: function(layero){
		 	        	//重载表单
		 	        	 table.reload('idTest2', {
		 	     	        page: {
		 	     	          curr: 1 //重新从第 1 页开始
		 	     	        },where: {
		 	     	        	purId: obj.data.purId,
								}
		 	     	      });
		 	        },end : function() {
		 	        	$("#add").hide();
		 	        }
		 	    });
			  }else if(obj.event === 'shxq'){
				  layer.open({
		   	        type: 1	 //此处以iframe举例
		   	        ,title: '审核详情'
		   	        ,offset: 'auto'
		   	        ,area: ['1000px', '490px']
		   	        ,content:$('#add4')
		   	        ,success: function(layero){
		   	        	//重载表单
		   	        	 table.reload('idTest4', {
		   	     	        page: {
		   	     	          curr: 1 //重新从第 1 页开始
		   	     	        },where: {
		   	     	        	purId: obj.data.purId,
								}
		   	     	      });
		   	        },end : function() {
		   	        	$("#add4").hide();
		   	        }
		   	    });
			  }else if(obj.event === "shenhe"){
				  var index = layer.open({
		    	        type: 1	 //此处以iframe举例
		    	        ,title: '总公司经理审核'
		    	        ,offset: 'auto'
		    	        ,area: ['450px', '300px']
		    	        ,content:$('#add3')
		    	        ,success: function(layero){
		    	        	$("#zuihoutijiao").click(function(){
		    	        		var index2 = layer.load(0, {
	        						  shade: [0.5,'#000'] //0.1是透明度      白色背景
		        				  });
				        		if($("#shenheyijian").val()!=''){
				        		  var url="../../sumchase/findByinventoryAndFinance.action";
				        		  var data={"purId":obj.data.purId,"auState":$("input[name='sex']:checked").val(),"auIdea":$("#shenheyijian").val(),"purSum":obj.data.purSum,"braId":obj.data.braId};								  
				        		  $.post(url,data,function(mes){
				        			  if(mes==1){
				    	      			  layer.close(index);//关闭当前页
					    	      			table.reload('idTest', {
										        page: {
										          curr: 1 //重新从第 1 页开始
										        }
					    	      			})
					    	      			layer.msg('审核成功', {icon: 1});
				        			  }else{
				        				  layer.msg('审核失败', {icon: 2});
				    	      			  layer.close(index);//关闭当前页
				    	      			  table.reload('idTest', {
									        page: {
									          curr: 1 //重新从第 1 页开始
									        }
				    	      			  })
				        			  }
				        		  },"json");
				        		}else{
		    	        			$("#shenheyijian").val("");
		    	        			layer.msg('请填写审核意见', {icon: 2});
		    	        		}
				        		layer.close(index2);//关闭当前页
				        	});
		    	        	
		    	        },end : function() {
			  	        	$("#add3").hide();
			  	        	$("#shenheyijian").val("");
			  	        }
			  	    });
			  }
	  })
	 
	 
	 
	 
	 
	 
	 
	//日期
	  laydate.render({
	    elem: '#date'
	  });
	  
	  laydate.render({
		    elem: '#time'
	  });
	  
	
	//采购表条件查询
	  var $ = layui.$, active = {
	    reload: function(){
	      var date = $('#date');
	      var time = $('#time');
	      var braId = $('#braId');
	      //执行重载
	      table.reload('idTest', {
	        page: {
	          curr: 1 //重新从第 1 页开始
	        }
	        ,where: {
	        	//把值赋给模型类
	        	optime: date.val(),
	        	opdate: time.val(),
	        	braId: braId.val()
	        }
	      });
	    }
	  };
	  
	  //查询
	  $('.demoTable .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		    
	  });
})	

//表单渲染 
function load() {
	 var form = layui.form
	 form.render();
}